<template>
  <div class="login-container">
    <h1>教务管理系统登录</h1>
    <el-form :model="loginForm" label-width="80px">
      <el-form-item label="用户类型">
        <el-radio-group v-model="loginForm.userType">
          <el-radio-button label="student">我是学生</el-radio-button>
          <el-radio-button label="teacher">我是老师</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="学工号">
        <el-input v-model="loginForm.userId"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="loginForm.password" type="password"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="login">登录</el-button>
    <p style="text-align: center; font-size: 12px; color: #666; margin-top: 10px;">首次登录将使用所输入的密码自动注册</p>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useUserStore } from '@/stores/user';
import { ElForm, ElFormItem, ElInput, ElButton, ElRadioButton, ElRadioGroup } from 'element-plus';
import router from '@/router';
const loginForm = ref({
  userType: 'student', // 新增用户类型字段，用于存储选择的用户类型
  userId: '',
  password: ''
});

const userStore = useUserStore();
const login = async () => {
  try {
    await userStore.logIn(loginForm.value);
    router.push("/overview");
  } catch (error) {
    // 处理登录失败的情况，例如显示错误信息
    console.error('登录失败:', error);
  }
};
</script>

<style scoped>
.login-container {
  width: 300px;
  margin: 100px auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>